﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using iTextSharp.text;
using iTextSharp.text.pdf;

namespace iPdf
{
    class SinglePdfManipulator<T>: SimpleActor<T>
    {
        protected string outputFileName;
        protected FileStream destFileStream;
        protected PdfStamper stamper;

        protected override void NullfyObjects()
        {
            base.NullfyObjects();
            destFileStream = null;
            stamper = null;
        }

        protected virtual void OpenStamper()
        {
            stamper = new PdfStamper(reader, destFileStream);
        }


        protected override void PreProcess()
        {
            outputFileName = Helper.GetTemporaryFile(Global.PdfExt);
            destFileStream = Helper.OpenWrite(outputFileName);
            OpenStamper();
        }

        protected override void PostProcess()
        {
            //copy back to replace the original file
            Helper.CopyFile(outputFileName, workItem.Source);
        }

        protected override void ClosePdf()
        {
            SharpHelper.ClosePdf(stamper, destFileStream, reader, null, true);
        }

    }
}
